package org.cnam.mobileShopping.bean;

import java.text.ParseException;
import java.util.Date;

import org.cnam.mobileShopping.wsclient.WSEntityListId;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

@WSEntityListId("productlist")
public class Productlist {
	private static final long serialVersionUID = 1L;

	private Integer mlistid;

	private Integer productlistid;

	private Product product;

	private int quantity;

	private Integer statusid;

	private Integer userid;

	private Integer productid;

	private Date lastupdatedatetime;

	private String commonid;

	private int version;

	private boolean issynchr;

	public Integer getProductlistid() {
		return productlistid;
	}

	public void setProductlistid(Integer productlistid) {
		this.productlistid = productlistid;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public Integer getStatusid() {
		return statusid;
	}

	public void setStatusid(Integer statusid) {
		this.statusid = statusid;
	}

	/**
	 * 
	 * @param mlistid
	 * @param productlistid
	 * @param quantity
	 * @param statusid
	 * @param userid
	 * @param productid
	 * @param lastupdatedatetime
	 */
	public Productlist(Integer mlistid, Integer productlistid, int quantity,
			Integer statusid, Integer userid, Integer productid,
			Date lastupdatedatetime) {
		super();
		this.mlistid = mlistid;
		this.productlistid = productlistid;
		this.quantity = quantity;
		this.statusid = statusid;
		this.userid = userid;
		this.productid = productid;
		this.lastupdatedatetime = lastupdatedatetime;
	}

	public Productlist() {
	};

	public static Productlist parseJSON(JSONObject jsonObject)
			throws JSONException {
		Productlist toReturn = null;
		Date d;
		try {
			d = Constants.formatDateEN.parse(jsonObject
					.getString("lastupdatedatetime"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			Log.e("MList", "error on parse lastupdatedatetime");
			d = new Date();
		}
		toReturn = new Productlist(jsonObject.getInt("mlistId"),
				jsonObject.getInt("productlistid"),
				jsonObject.getInt("quantity"), jsonObject.getInt("statusid"),
				jsonObject.getInt("userid"), jsonObject.getInt("productid"), d);
		toReturn.setCommonid(jsonObject.getString("commonid"));
		toReturn.setVersion(jsonObject.getInt("version"));
		return toReturn;
	}

	public Integer getMlistid() {
		return mlistid;
	}

	public void setMlistid(Integer mlistid) {
		this.mlistid = mlistid;
	}

	public Integer getUserid() {
		return userid;
	}

	public void setUserid(Integer userid) {
		this.userid = userid;
	}

	public Integer getProductid() {
		return productid;
	}

	public void setProductid(Integer productid) {
		this.productid = productid;
	}

	public Date getLastupdatedatetime() {
		return lastupdatedatetime;
	}

	public void setLastupdatedatetime(Date lastupdatedatetime) {
		this.lastupdatedatetime = lastupdatedatetime;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Product getProduct() {
		return product;
	}

	@Override
	public String toString() {
		String s = null;
		if (product != null) {
			s = product.toString() + " (qté: " + quantity;
			if (statusid == Constants.statusProductInList.BUY.getId()) {
				s += ", ACHETÉ";
			}
			s += ")";
		} else {
			s = "no product available";
		}
		return s;

	}

	public String getCommonid() {
		return commonid;
	}

	public void setCommonid(String commonid) {
		this.commonid = commonid;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public boolean isSynchr() {
		return issynchr;
	}

	public void setSynchr(boolean isSynchr) {
		this.issynchr = isSynchr;
	}
}
